- /* slflpow.cpp by K.Tsuru */
- // function ID 2002 DRADIX, BRADIX
- /**************************************************************
- SLong and SInteger classes
- It provides to the n-th powewr of x(x^n) Knuth's binary method.
- ***************************************************************/
- #ifndef SN_H
- #include "sn.h"
- #endif
- #include <iostream>
-
- SLong Lpow(const SLong& x, ulong n){
- if(!x.Sign(2002)) return x; // x = 0
- int sgn = 1;
- uint h = x.Head();
- if(x.Sign() < 0) sgn = (n & 1) ? -1 : 1;
-
- if(!n) return SLong(x.Type(), minArraySize, 1L); // return 1;
- if(x.IsOne()) return SLong(x.Type(), minArraySize, sgn); // |x| = 1;
-
- fType rdx = x.Radix();
- //It estimates the number of figures which needs to store the result.
- //x has an order of x[h]*(R^h).
- ulong fig;
- fig = ulong( (double)n*(log10((double)x[h])/log10((double)rdx) + (double)h) )+1u;
- //figures in radix
-
- if(fig > x.MaxSize()) {
- cerr << "fig = " << fig << " n= " << n << endl;
- x.SetError(x.OVERFLOW_ERR,"Lpow", 2002);
- }
- /*
- The necessary figues is known, then it allocates its memory.
- If the out of memory error occures the program terminates here.
- */
- SLong y( x.Type(), (uint)fig), z( x.Type(), (uint)fig); // cutDown = DISABLE
- y = 1; z = x; //To make the size same as that of y it does not use z(x).
- if(x.Sign() < 0) z.ChangeSign(); // z > 0
-
- while(1){
- if( n & 1 ) y *= z;
- n /= 2;
- if( !n ) break;
- z *= z;
- }
- if(sgn < 0) y.ChangeSign();
- return y;
- }
slflpow.cpp : last modifiled at 2017/03/13 14:32:00(1,488 bytes)
created at 2017/10/07 10:26:50
The creation time of this html file is 2017/11/09 14:52:03 (Thu Nov 09 14:52:03 2017).